//
//  LDClassTabCellOne.m
//  Gift
//
//  Created by 李迪 on 15/11/11.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDClassTabCellOne.h"
#import "BaseHeader.h"
#import "LDClassCell.h"
@interface LDClassTabCellOne ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)NSMutableArray *shujuArray;


@end

@implementation LDClassTabCellOne


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self handleData];
    }
    return self;
}

- (void)craretaCollection{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 4 - 20, SCREEN_WIDTH / 4 + 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) collectionViewLayout:flowLayout];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.contentSize = CGSizeMake(0, 0);
    [self.collection setBackgroundColor:[UIColor whiteColor]];
    [self.contentView addSubview:self.collection];
    [self.collection registerClass:[LDClassCell class] forCellWithReuseIdentifier:@"LDClassCell"];
    
    //夜间
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
        self.collection.backgroundColor = [UIColor blackColor];

    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNot:) name:@"lidi" object:nil];
    
}

//夜间方法
- (void)handleNot:(NSNotification *)Not{
    NSString *passWord = [Not object];
    if ([passWord isEqualToString:@"yes"]) {
        self.backgroundColor = [UIColor blackColor];
        self.collection.backgroundColor = [UIColor blackColor];

    }else if ([passWord isEqualToString:@"no"]){
        self.collection.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor whiteColor];

    }
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{


    return [self.shujuArray[self.Section] count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LDClassCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"LDClassCell" forIndexPath:indexPath];
    
    self.model = [self.shujuArray[self.Section] objectAtIndex:indexPath.row];
    item.model = self.model;
    return item;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(70, 70);
}

- (void)handleData{
    
    
    self.shujuArray = [[NSMutableArray array] init];
    [PFAFNetworking getUrl:@"http://api.liwushuo.com/v2/channel_groups/all" body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.dataArr = [[NSMutableArray array] init];

        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"] objectForKey:@"channel_groups"]) {
            self.dataArr = [[NSMutableArray alloc]init];
            for (NSDictionary *dataDic in [dic objectForKey:@"channels"]) {
                ClassificationModel *moDel = [ClassificationModel modelSetDic:dataDic];
                [self.dataArr addObject:moDel];
            }
            [self.shujuArray addObject:self.dataArr];
        
            [_model release];
        }
        [self craretaCollection];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}





-(void)setSection:(NSInteger)Section
{
    _Section = Section;
    [self.collection reloadData];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.collection.frame = CGRectMake(0, 30, SCREEN_WIDTH, self.contentView.frame.size.height - 30);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassificationModel *model = [self.shujuArray[self.Section] objectAtIndex:indexPath.row];
 
    [self.delegat pushViewCid:model.Cid];


}







- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
